﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace AjaxControlToolkit
{
    [ParseChildren(true)]
    [RequiredScript(typeof(CommonToolkitScripts))]
    [RequiredScript(typeof(SideBar))]
    [ClientCssResource("AjaxControlToolkit.SideBar.SideBar.css")]
    [ClientScriptResource("AjaxControlToolkit.SidePanel", "AjaxControlToolkit.SideBar.SideBar.js")]
    [ToolboxItem(false)]
    public class SidePanel : ScriptControlBase
    {
        private ITemplate _contentTemplate;
        private Image _panelImage;
        private SideBar _owner;
        private bool _active;

        public SidePanel()
            : base(false)
        {
            _panelImage = new Image();
        }

        internal bool Active
        {
            get { return _active; }
            set { _active = value; }
        }

        [DefaultValue("")]
        [UrlProperty]
        [Category("Appearance")]
        public string PanelImage
        {
            get { return _panelImage.ImageUrl; }
            set { _panelImage.ImageUrl = value; }
        }

        [DefaultValue("")]
        [Category("Appearance")]
        [ExtenderControlProperty]
        [ClientPropertyName("panelText")]
        public string PanelText
        {
            get { return (string)(ViewState["PanelText"] ?? string.Empty); }
            set { ViewState["PanelText"] = value; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        [Browsable(false)]
        [MergableProperty(false)]
        public ITemplate ContentTemplate
        {
            get { return _contentTemplate; }
            set { _contentTemplate = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (_contentTemplate != null)
            {
                Control c = new Control();
                _contentTemplate.InstantiateIn(c);
                Controls.Add(c);
            }
        }

        protected internal virtual void RenderPanel(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "__button_" + ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            if (!string.IsNullOrEmpty(PanelImage))
            {
                _panelImage.ID = "__button_" + ClientID + "_img";
                writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "block");
                _panelImage.RenderControl(writer);
            }
            writer.RenderEndTag();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            Unit h = new Unit(_owner.Height.Value - _owner.CaptionHeight.Value);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, h.ToString());

            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Visibility, "hidden");

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            RenderChildren(writer);
            writer.RenderEndTag();
            ScriptManager.RegisterScriptDescriptors(this);
        }

        protected override void DescribeComponent(ScriptComponentDescriptor descriptor)
        {
            base.DescribeComponent(descriptor);
            descriptor.AddElementProperty("button", "__button_" + ClientID);
            if (_owner != null)
            {
                descriptor.AddComponentProperty("owner", _owner.ClientID);
            }
        }


        internal void SetOwner(SideBar owner)
        {
            _owner = owner;
        }

    }
}
